// miniprogram/pages/index/index.js
import {
  IndexModel
} from '../../model/index'
import {MapModel} from '../../model/map'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerBg: '/images/banner-bg.png',
    bannerList: [],
    newsList: [1, 2, 3, 4],
    orgList: [{
      "img": '/images/1.png',
      "txt": '线上活动'
    }, {
      "img": '/images/2.png',
      "txt": '线下活动'
    }, {
      "img": '/images/3.png',
      "txt": '微心愿'
    }, {
      "img": '/images/4.png',
      "txt": '风采展示'
    }],
    learnList: [{
      "img": '/images/5.png',
      "txt": '学习视频'
    }, {
      "img": '/images/6.png',
      "txt": '学习资料'
    }, {
      "img": '/images/7.png',
      "txt": '我要答题'
    }],
    mapList: [{
      "img": '/images/8.png',
      "txt": '新龙大厦'
    }, {
      "img": '/images/9.png',
      "txt": '金燕龙大厦'
    }, {
      "img": '/images/10.png',
      "txt": '金域国际'
    }, {
      "img": '/images/11.png',
      "txt": '龙旗广场'
    }],
    myList: [{
      "img": '/images/13.png',
      "txt": '我的资料'
    }, {
      "img": '/images/14.png',
      "txt": '我的点赞'
    }, {
      "img": '/images/15.png',
      "txt": '我的活动'
    }, {
      "img": '/images/16.png',
      "txt": '我的课程'
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadBanner()
    this.loadNewsList()
    this.loadBuildingData()
  },
  loadBanner() {
    IndexModel.bannerList().then((res) => {
      // console.log(res)
      if (res && res.code == 200) {
        this.setData({
          bannerList: res.rows
        })
      } else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  loadNewsList() {
    IndexModel.dictList('sys_news_type').then((res) => {
      // console.log(res)
      if (res && res.code == 200) {
        this.setData({
          newsList: res.rows
        })
      } else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  loadBuildingData(){
    MapModel.buildingList().then((res) => {
      if(res && res.code == 200) {
        this.data.mapList = res.data.map(res => {
          return {
            id: res.buildingId,
            img: res.buildingImg,
            txt: res.buildingName
          }
        })
        this.setData({
          mapList: this.data.mapList.slice(0,20)
        })
        // console.log('building', res.data, this.data.mapList)
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  jump(e) {
    wx.navigateTo({
      url: `../news/index?opt=${e.detail.id+1}`,
    })
  },
  go(e) {
    // console.log('index', e.detail.id, e.detail.type)
    let {
      type,
      id
    } = e.detail
    if (type === "1") { // org
      if(id === 2){
        wx.navigateTo({
          url: `../org/index?type=1`,
        })
      }else if(id ==0 || id==1) {
        wx.navigateTo({
          url: `../org/index?activityType=${e.detail.id+1}`,
        })
      } else {
        wx.switchTab({
          url: `../org/org`,
        })
      }

    } else if (type === "2") { // learn
      wx.navigateTo({
        url: `../learn/index?type=${id+1}`,
      })
    } else if (type === "3") { // map
      let bid = this.data.mapList[id].id
      wx.navigateTo({
        url: `../map/build/index?id=${bid}`,
      })
    } else {
      switch (id) {
        case 0:
          wx.navigateTo({
            url: `../my/data/index`,
          })
          break;
        case 1:
          wx.navigateTo({
            url: `../my/dig/index`,
          })
          break;
        case 2:
          wx.navigateTo({
            url: `../my/activity/index`,
          })
          break;
        case 3:
          wx.navigateTo({
            url: `../my/course/index`,
          })
          break;
      }

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})